library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;

entity led8 is
  generic(n:integer :=50000000);   
  port (
        clk:in std_logic;
        reset:in std_logic;
		  regular:in std_logic;
		  random:in std_logic; 
        led8s:out std_logic_vector(9 downto 0);
		  led10s:out std_logic_vector(9 downto 0));
end led8;
 
architecture behave of led8 is
  signal count :integer range n-1 downto 0:=n-1;   --给count赋初值n-1
  signal q1 :std_logic;
  signal q2 :std_logic;
  signal count1 :std_logic_vector(3 downto 0);
  signal count2 :std_logic_vector(3 downto 0);
  
begin

process(clk)       --分频，分成1Hz
 begin
 if rising_edge(clk) then   --如果是时钟的上升沿则
     count<=count-1;     --4999999
     if count>=n/2 then    
--	  if (count=n/value/2-1) then
--	    count <= x"00000000";
--		 q1 <= not q1;
       q1<='0';
     else
       q1<='1';
     end if;
     if count<=0 then
       count<=n-1;
     end if;
  end if;
end process;

process(q1)      
begin
  if regular='0' then 
    count1<="0000";
  elsif rising_edge(q1) then    
    if count1="1001" then
      count1<="0000";
    else
     count1<=count1+1;    --计数器+1就是对应灯的移位
    end if;
  end if;
end process;

process(count1)
begin
  case count1 is      --计数器选灯
     when "0000" => led10s<="1111111110";   --哪个位是0就是哪个led灯灭
     when "0001" => led10s<="1111111101";
     when "0010" => led10s<="1111111011";
     when "0011" => led10s<="1111110111";
     when "0100" => led10s<="1111101111";
     when "0101" => led10s<="1111011111";
     when "0110" => led10s<="1110111111";
     when "0111" => led10s<="1101111111";
	  when "1000" => led10s<="1011111111";
	  when "1001" => led10s<="0111111111";
	  when others => led10s<="0000000000";
   end case;
end process;

process(q2)      
begin
  if random='0' then 
    count2<="0000";
  elsif rising_edge(q2) then    
    if count2="1001" then
      count2<="0000";
    else
     count2<=count2+2;    --计数器+2就是对应灯的移位
    end if;
  end if;
end process;

process(count2)
begin
  case count2 is      --计数器选灯
     when "0000" => led8s<="1111111110";   --哪个位是0就是哪个led灯灭
     when "0001" => led8s<="1111111101";
     when "0010" => led8s<="1111111011";
     when "0011" => led8s<="1111110111";
     when "0100" => led8s<="1111101111";
     when "0101" => led8s<="1111011111";
     when "0110" => led8s<="1110111111";
     when "0111" => led8s<="1101111111";
	  when "1000" => led8s<="1011111111";
	  when "1001" => led8s<="0111111111";
	  when others => led8s<="0000000000";
   end case;
end process;
end behave;

